﻿using System;
using System.Collections.Generic;
using System.Collections.Concurrent;

using RexToy.DesignPattern;

namespace RexToy.Cassandra.Meta
{
    public class MetaCache : Singleton<MetaCache>
    {
        ConcurrentDictionary<string, KeyspaceInfo> _dict;
        private MetaCache()
        {
            _dict = new ConcurrentDictionary<string, KeyspaceInfo>();
        }

        internal ColumnFamilyInfo GetMeta(string keyspace, string columnFamily, Connection cnn)
        {
            if (_dict.ContainsKey(keyspace))
            {
                return _dict[keyspace].ColumnFamilyInfos[columnFamily];
            }
            else
            {
                var ksDef = cnn.Client.describe_keyspace(keyspace);
                KeyspaceInfo ks = new KeyspaceInfo(ksDef);
                _dict[keyspace] = ks;
                return ks.ColumnFamilyInfos[columnFamily];
            }
        }
    }
}
